﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using System.ServiceModel;
using PJ.Framework.Common;
using PJ.Data.Partner;
using PJ.Framework;
using PJ.Data.Inventory;
using PJ.Data.SalePurchase.Sale;
using PJ.Business.SalePurchase.Sale;
using PJ.Operation.SalePurchase.Sale;

namespace PJ.KrystalServer
{
    /// <summary>
    /// Logique d'interaction pour App.xaml
    /// </summary>
    public partial class App : Application
    {
        ServiceHost _CustomerProviderHost;
        ServiceHost _InventoryItemProviderHost;
        ServiceHost _InventoryBarCodeProviderHost;
        ServiceHost _InventoryItemSalesParamProviderHost;
        ServiceHost _SaleInvoiceProviderHost;
        ServiceHost _SaleInvoiceServiceHost;


        protected override void OnStartup(StartupEventArgs e)
        {
            try
            {
                InitContainer();
                base.OnStartup(e);
                _CustomerProviderHost = new ServiceHost(typeof(CustomerProvider));
                _CustomerProviderHost.Open();
                _InventoryItemProviderHost = new ServiceHost(typeof(InventoryItemProvider));
                _InventoryItemProviderHost.Open();
                _InventoryItemSalesParamProviderHost = new ServiceHost(typeof(InventoryItemSalesParamProvider));
                _InventoryItemSalesParamProviderHost.Open();
                _InventoryBarCodeProviderHost = new ServiceHost(typeof(InventoryBarCodeProvider));
                _InventoryBarCodeProviderHost.Open();
                _SaleInvoiceProviderHost = new ServiceHost(typeof(SaleInvoiceProvider));
                _SaleInvoiceProviderHost.Open();
                _SaleInvoiceServiceHost = new ServiceHost(typeof(SaleInvoiceService));
                _SaleInvoiceServiceHost.Open();
                Log.WriteApplicationStart();
            }
            catch (Exception ex)
            {
                ExceptionManager.Handle(ex);
                MessageBox.Show(ex.Message);
            }
        }

        protected override void OnExit(ExitEventArgs e)
        {
            try
            {
                Log.WriteApplicationStop();
                if (_CustomerProviderHost != null && _CustomerProviderHost.State == CommunicationState.Opened)
                    _CustomerProviderHost.Close();
                if (_InventoryBarCodeProviderHost != null && _InventoryBarCodeProviderHost.State == CommunicationState.Opened)
                    _InventoryBarCodeProviderHost.Close();
                if (_InventoryItemProviderHost != null && _InventoryItemProviderHost.State == CommunicationState.Opened)
                    _InventoryItemProviderHost.Close();
                if (_InventoryItemSalesParamProviderHost != null && _InventoryItemSalesParamProviderHost.State == CommunicationState.Opened)
                    _InventoryItemSalesParamProviderHost.Close();
                if (_SaleInvoiceServiceHost != null && _SaleInvoiceServiceHost.State == CommunicationState.Opened)
                    _SaleInvoiceServiceHost.Close();
                if (_SaleInvoiceProviderHost != null && _SaleInvoiceProviderHost.State == CommunicationState.Opened)
                    _SaleInvoiceProviderHost.Close();
                base.OnExit(e);
            }
            catch (Exception ex)
            {
                ExceptionManager.Handle(ex);
                MessageBox.Show(ex.Message);
            }
        }

        private void InitContainer()
        {
            InjectionBroker.RegisterType<ICustomerProvider, CustomerAXProvider>();
            InjectionBroker.RegisterType<ICustomerCache<ICustomerData>, CustomerCache>();

            InjectionBroker.RegisterType<IInventoryItemProvider, InventoryItemAXProvider>();
            InjectionBroker.RegisterType<IInventoryItemCache<IInventoryItemData>, InventoryItemCache>();
            
            InjectionBroker.RegisterType<IInventoryBarCodeProvider, InventoryBarCodeAXProvider>();
            InjectionBroker.RegisterType<IInventoryBarCodeCache<IInventoryBarCodeData>, InventoryBarCodeCache>();

            InjectionBroker.RegisterType<IInventoryItemSalesParamProvider, InventoryItemSalesParamAXProvider>();
            InjectionBroker.RegisterType<IInventoryItemSalesParamCache<IInventoryItemSalesParamData>, InventoryItemSalesParamCache>();

            InjectionBroker.RegisterType<ISaleInvoiceProvider, SaleInvoiceAXProvider>();
            InjectionBroker.RegisterType<ISaleInvoiceCache<ISaleInvoiceData>, SaleInvoiceCache>();

            InjectionBroker.RegisterType<ICreateSaleInvoice, AxCreateSaleInvoice>();
        }
    }
}
